﻿var treeMenu = null;
var setting = {
    data: {
        simpleData: {
            enable: true
        },
    },
    async: {
        enable: true,
        url: "/UserInfo/Home/MenuList",
        type: "post",
    },
    view: {
        showLine: false,
        showIcon: false,
        selectedMulti: false,
        dblClickExpand: false,
        addDiyDom: addDiyDom
    },
    callback: {
        beforeClick: treeMenuBeforeClick,
        onAsyncSuccess: treeMenuOnAsyncSuccess,
    }
};
$(function () {
    treeMenu = $.fn.zTree.init($("#treeMenu"), setting);
});

/**************************Ztree相关的函数***********************************************/
function treeMenuOnAsyncSuccess() {
    navbarClick();
    PageHeaderContextMenu();
}
function treeMenuBeforeClick(treeId, treeNode) {
    var zTree = $.fn.zTree.getZTreeObj("treeMenu");
    zTree.expandNode(treeNode);
    var tid = treeNode.tId;
    var href = $("#" + tid + " a:first").attr("href");
    if (typeof (href) != "undefined") {
        $("#" + tid + " a:first").attr("data-href", href);
        $("#" + tid + " a:first").attr("data-title", treeNode.name);
        $("#" + tid + " a:first").removeAttr("href");
    }
    return true;
}
function addDiyDom(treeId, treeNode) {
    var spaceWidth = 10;
    var switchObj = $("#" + treeNode.tId + "_switch"),
    icoObj = $("#" + treeNode.tId + "_ico");
    switchObj.remove();
    icoObj.before(switchObj);

    if (treeNode.level > 1) {
        var spaceStr = "<span style='display: inline-block;width:" + (spaceWidth * (treeNode.level - 1)) + "px'></span>";
        switchObj.before(spaceStr);
    }
}
/**************************Ztree相关的函数***********************************************/
function navbarClick() {//顶部导航栏点击事件方法
    $("#welcome").click(function () {
        $(".Hui-aside a").each(function () {
            var item = this;
            if ($(item).html().trim() == "首页") {
                $(item).click();
            }
        });
    });
};
function PageHeaderContextMenu() {//页头右击菜单
    txy.ui.hui_PageHeaderContextMenuReset();
    $(".Hui-aside a").click(function () {
        setTimeout(txy.ui.hui_PageHeaderContextMenuReset, 20); //间隔20ms，先让框架生成新增的页头
    });
};
